package com.garen.fire.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.garen.common.BaseAction;
import com.garen.common.MsgBean;
import com.garen.common.PageBean;
import com.garen.fire.biz.IFireCaseInfoBiz;
import com.garen.fire.entity.FireCaseInfo;
import com.garen.sys.biz.IFireOrgBiz;
import com.garen.sys.entity.FireUser;
import com.garen.sys.filter.SysFilter;
import com.garen.utils.CommonUtils;
import com.garen.utils.DateUtils;
import com.garen.utils.FileUtils;


@Controller
@RequestMapping("/fire")
public final class FireCaseInfoAction extends BaseAction{

	

	@Autowired
	private IFireCaseInfoBiz iFireCaseInfoBiz;
	
	@Autowired
	private IFireOrgBiz iFireOrgBiz;
	

	//录音获取
	@RequestMapping("/firecaseinfo/casesound") 
	public ModelAndView caseSound(String ajbh) {
		byte[] sound = iFireCaseInfoBiz.caseSound(ajbh);
		if(null == sound) {//如果声音为空，加入默认测试声音。
			sound = FileUtils.readData(CommonUtils.getRealPath("/temp/a.wav"));
		}
		return downWav(sound);
	}
	
	
	//验证录音是否存在
	@RequestMapping("/firecaseinfo/checksound") 
	public ModelAndView checkSound(String ajbh) {
		MsgBean mbean = SysFilter.getMsgBean();
		if(StringUtils.isEmpty(ajbh)) {
			mbean.setRetInfo(-1, "参数不能为空!");
		}else {
			JSONObject json = new JSONObject();
			json.put("sound",iFireCaseInfoBiz.queryCaseSound(ajbh));
			mbean.setObj(json);
		}
		return Json(mbean);
	}
		
	
	//获取案情最新位置信息
	@RequestMapping("/firecaseinfo/gps") 
	public ModelAndView gps(String ajbh) {
		MsgBean mbean = SysFilter.getMsgBean();
		mbean.setObj(iFireCaseInfoBiz.getFireCaseGps(ajbh));
		return Json(mbean);
	}
	//获取案情最新位置信息
	@RequestMapping("/firecaseinfo/caseinfobycar") 
	public ModelAndView caseinfobycar(String carid) {
		MsgBean mbean = SysFilter.getMsgBean();
		mbean.setObj(iFireCaseInfoBiz.getCaseInfoByCar(carid));
		return Json(mbean);
	}                                                      
 
	/*
	 * 加入查询条件
	 * 1 时间
	 *   若时间为空，则取最近12小时案情
	 *   若不为空，则取大于此时间的
	 */
	@RequestMapping("/firecaseinfo/jsonEx") 
	public ModelAndView jsonEx(FireCaseInfo fireCaseInfo) {
		MsgBean mbean = SysFilter.getMsgBean();
		FireUser user = getSessionUser();
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.HOUR,(int)(-12));
		fireCaseInfo.setBjsj(cale.getTime());
		Integer orgId = fireCaseInfo.getOrgId();
		if(orgId == null) orgId = user.getUserOrgId();
		mbean.setObj(iFireCaseInfoBiz.getFireCaseInfoList(fireCaseInfo,orgId));
		return Json(mbean);
	}
	
	@RequestMapping("/firecaseinfo/jsonExPad") 
	public ModelAndView jsonExPad(FireCaseInfo fireCaseInfo) {
		MsgBean mbean = SysFilter.getMsgBean();
		FireUser user = getSessionUser();
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.HOUR,(int)(-12));
		fireCaseInfo.setBjsj(cale.getTime());
		Integer orgId = fireCaseInfo.getOrgId();
		if(orgId == null) orgId = user.getUserOrgId();
		mbean.setObj(iFireCaseInfoBiz.getFireCaseInfoListPad(fireCaseInfo,orgId));
		return Json(mbean);
	}
	/*
	 * 当天的不同状态下一个org下的不同状态案件个数
	 */
	@RequestMapping("/firecaseinfo/statuscount") 
	public ModelAndView statuscount(FireCaseInfo fireCaseInfo,Integer org_id) {
		MsgBean mbean = SysFilter.getMsgBean();
		FireUser user = getSessionUser();
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.HOUR,(int)(-12));
		fireCaseInfo.setBjsj(cale.getTime());
		 
		 
		mbean.setObj(iFireCaseInfoBiz.statuscount(fireCaseInfo,org_id));
		return Json(mbean);
	}
	
	@RequestMapping("/firecaseinfo/carstatuscount") 
	public ModelAndView carStatusCount(Integer orgId) {
		List<Integer> orgIdList = iFireOrgBiz.getOrgChildrenID(orgId);
		MsgBean mbean = SysFilter.getMsgBean();
		mbean.setObj(iFireCaseInfoBiz.getCarCountByOrgId(orgIdList));
		return Json(mbean);
	}
	
	/*
	 * 时间段查询
	 */
	@RequestMapping("/firecaseinfo/hisjsons") 
	public ModelAndView hisJsons(PageBean<FireCaseInfo> pb,Date kssj,Date jssj) {
		if(kssj == null || jssj == null){
			pb.setRetInfo(-1,"参数不正确!");
		}else{
			FireUser user = getSessionUser();
			Integer orgId = user.getUserOrgId();
			kssj = DateUtils.clearDate(kssj);
			jssj = DateUtils.fullDate(jssj);
			pb.setRows(iFireCaseInfoBiz.getFireCaseInfoList(pb,orgId,kssj,jssj));
		}
		return Json(pb);
	}
	
	/*
	 * 更新位置
	 * */
	@RequestMapping("/firecaseinfo/updatepos") 
	public ModelAndView updatePos(FireCaseInfo fireCaseInfo) {
		MsgBean mbean = SysFilter.getMsgBean(0, "保存成功 !");
		if(fireCaseInfo.getCaseId() == null
				|| fireCaseInfo.getGpsLat() == null
				|| fireCaseInfo.getGpsLng() == null){
			mbean.setRetInfo(-1, "参数不正确!");
		}else {
			fireCaseInfo.setGpsType(FireCaseInfo.GPS_TYPE_TEAM);
			iFireCaseInfoBiz.updateFireCaseInfo(mbean,fireCaseInfo);
		}
		return Json(mbean);
	}
	
	@RequestMapping("/firecaseinfo/save") 
	public ModelAndView save(FireCaseInfo fireCaseInfo) {
		MsgBean mbean = SysFilter.getMsgBean(0, "保存成功 !");
		iFireCaseInfoBiz.saveFireCaseInfo(mbean,fireCaseInfo);
		return Json(mbean);
	}
		
	@RequestMapping("/firecaseinfo/json") 
	public ModelAndView json(PageBean<FireCaseInfo> pb,FireCaseInfo fireCaseInfo) {
		//如果为空字符串，则设置为null
		if("".equals(fireCaseInfo.getZgzd())) fireCaseInfo.setZgzd(null);
		iFireCaseInfoBiz.getFireCaseInfoList(pb,fireCaseInfo);
		return Json(pb);
	}
	
	@RequestMapping("/firecaseinfo/copycase") 
	public ModelAndView copyCase(FireCaseInfo fireCaseInfo) {
		MsgBean mbean = SysFilter.getMsgBean();
		iFireCaseInfoBiz.copyCase(mbean, fireCaseInfo);
		return Json(mbean);
	}
	
	
}
